//
//  MapCenterView.m
//  MyAlert
//
//  Created by 美鑫科技 on 16/5/3.
//  Copyright © 2016年 美鑫科技. All rights reserved.
//

#import "MapCenterView.h"
#import "MapCenterCalloutView.h"

@interface MapCenterView ()

@property (nonatomic, strong) MapCenterCalloutView *calloutView;
@property (nonatomic, strong) UIImageView *bacImageView;

@end

@implementation MapCenterView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.calloutView = [[MapCenterCalloutView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * 10.0 , 0)];
        self.calloutView.center = CGPointMake(self.frame.size.width / 2.0, -0);
        self.bacImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:self.bacImageView];
        [self addSubview:self.calloutView];
    }
    return self;
}

- (void)setBacImage:(UIImage *)bacImage{
    if (_bacImage != bacImage) {
        _bacImage = bacImage;
    }
    self.bacImageView.image = self.bacImage;
}

- (void)setTitle:(NSString *)title{
    if (_title != title) {
        _title = title;
    }
    _calloutView.title = title;
}

- (void)showCalloutView{
    [UIView beginAnimations:@"gogo" context:nil];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.1f];
    CGRect newFrame = CGRectMake(0, 0, self.frame.size.width *4, 40);
    self.calloutView.frame = newFrame;
    self.calloutView.center = CGPointMake(self.frame.size.width / 2.0, -23);
    [UIView commitAnimations];
    self.calloutView.hidden = NO;
}

- (void)dismissCalloutView{
    CGRect newFrame = CGRectMake(0, 0, self.frame.size.width *4, 0);
    self.calloutView.frame = newFrame;
    self.calloutView.center = CGPointMake(self.frame.size.width / 2.0, -0);
    self.calloutView.hidden = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    static int i = 0;
    if (i == 0) {
        [self showCalloutView];
        i = 1;
    } else if (i == 1){
        [self dismissCalloutView];
        i = 0;
    }
}

- (void)pullout{
    [UIView transitionWithView:self duration:0.5 options:(UIViewAnimationOptionTransitionNone) animations:^{
        CGRect newFrame =  self.frame;
        newFrame.origin.y = newFrame.origin.y - 8;
        self.frame = newFrame;
    } completion:nil];
}
- (void)insert{
    [UIView transitionWithView:self duration:0.5 options:(UIViewAnimationOptionTransitionNone) animations:^{
        CGRect newFrame =  self.frame;
        newFrame.origin.y = newFrame.origin.y + 8;
        self.frame = newFrame;
    } completion:nil];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
